package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.GoodsCreateBO;
import com.wangdao.wechatmall.bean.bo.admin.GoodsParamBO;
import com.wangdao.wechatmall.bean.pojo.Goods;
import com.wangdao.wechatmall.bean.vo.admin.*;
import com.wangdao.wechatmall.service.admin.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: JK
 * @Date: create in 2021/7/8 22:05
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     * 查询商品列表
     * @param param
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(GoodsParamBO param) {
        BaseDataVO<Goods> goodsGoodsBaseDataVO = goodsService.queryGoodsList(param);
        return BaseRespVo.ok(goodsGoodsBaseDataVO);
    }

    /**
     * 删除商品
     * @param goods
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Goods goods) {
        int result = goodsService.deleteGoods(goods);

        if (result == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }


    /**
     * 查询商品种类和所有品牌商
     */
    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        CatAndBrandVO catAndBrandVO = goodsService.showCatAndBrand();
        return BaseRespVo.ok(catAndBrandVO);
    }

    /**
     * 查询商品详情
     * @param id
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        GoodsDetailVO goodsDetailVO = goodsService.queryGoodsDetail(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    /**
     * 添加商品
     * @param goodsCreateBO
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GoodsCreateBO goodsCreateBO) {
        int result = goodsService.addGoods(goodsCreateBO);
        if (result == 1) {
            return BaseRespVo.ok();
        } else if (result == 401) {
            return BaseRespVo.fail("商品编号重复");
        }
        return BaseRespVo.fail();
    }

    /**
     * 更新商品
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GoodsCreateBO goodsCreateBO) {
        goodsService.updateGoods(goodsCreateBO);

        return BaseRespVo.ok();
    }
}
